//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using System.Security.Cryptography;
using Tathata.Common.Utils;
using System.Web.Security;
using System.Web;
using System.Collections.Specialized;
using Tathata.Common.Configuration;
using Tathata.Common.BLL.Entities;

namespace Tathata.Common.DAL.Managers
{
    internal class CustomerSessionDBManager
    {
        #region Methods
        public static CustomerSession GetByCustomerSessionGUID(Guid CustomerSessionGUID)
        {
            CustomerSession customerSession = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerSessionLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CustomerSessionGUID", DbType.Guid, CustomerSessionGUID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    customerSession = new CustomerSession();
                    customerSession.CustomerSessionGUID = NopDataHelper.GetGuid(dataReader, "CustomerSessionGUID");
                    customerSession.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    customerSession.LastAccessed = NopDataHelper.GetDateTime(dataReader, "LastAccessed");
                    customerSession.IsExpired = NopDataHelper.GetBoolean(dataReader, "IsExpired");
                }
            }

            return customerSession;
        }

        public static CustomerSession GetByCustomerID(int CustomerID)
        {
            CustomerSession customerSession = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerSessionLoadByCustomerID");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    customerSession = new CustomerSession();
                    customerSession.CustomerSessionGUID = NopDataHelper.GetGuid(dataReader, "CustomerSessionGUID");
                    customerSession.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    customerSession.LastAccessed = NopDataHelper.GetDateTime(dataReader, "LastAccessed");
                    customerSession.IsExpired = NopDataHelper.GetBoolean(dataReader, "IsExpired");
                }
            }

            return customerSession;
        }

        public static CustomerSession InsertCustomerSession(Guid CustomerSessionGUID, int CustomerID, DateTime LastAccessed, bool IsExpired)
        {
            CustomerSession customerSession = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerSessionInsert");
            db.AddInParameter(dbCommand, "CustomerSessionGUID", DbType.Guid, CustomerSessionGUID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "LastAccessed", DbType.DateTime, LastAccessed);
            db.AddInParameter(dbCommand, "IsExpired", DbType.Boolean, IsExpired);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                customerSession = GetByCustomerSessionGUID(CustomerSessionGUID);
            }
            return customerSession;
        }

        public static CustomerSession UpdateCustomerSession(Guid CustomerSessionGUID, int CustomerID, DateTime LastAccessed, bool IsExpired)
        {
            CustomerSession customerSession = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CustomerSessionUpdate");
            db.AddInParameter(dbCommand, "CustomerSessionGUID", DbType.Guid, CustomerSessionGUID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "LastAccessed", DbType.DateTime, LastAccessed);
            db.AddInParameter(dbCommand, "IsExpired", DbType.Boolean, IsExpired);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                customerSession = GetByCustomerSessionGUID(CustomerSessionGUID);

            return customerSession;
        }
        #endregion
    }
}
